﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DbHelp;

namespace IotherRis
{
    public partial class uc_IniCfg : UserControl
    {
        public uc_IniCfg()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                //验证判断
                string mError = "";
                bool mIsHave = false;
                if (string.IsNullOrEmpty(txt_pi_arges.Text))
                {
                    mError = mError + "\"使用参数\"不能为空\r\t";
                }


                if (!string.IsNullOrEmpty(mError))
                {
                    MessageBox.Show("REPORT错误:" + mError, "REPORT错误");
                    return;

                }
                //保存界面配置信息
                mIniCfg.MRelive_path = ckb_relative_path.Checked == true ? "1" : "0";
                mIniCfg.MPiview_column = txt_pi_arges.Properties.Items[txt_pi_arges.SelectedIndex].Value.ToString(); ;
                //mIniCfg.MPiview_arges = 
                //
                mIniCfg.MPiview_path = txt_piview_exe_path.Text.Trim();

                mIniCfg.MDbpath = txt_pi_db_path.Text.Trim();
                mIniCfg.MDbpath_report = txt_report_path.Text.Trim();
                mIniCfg.MPiview_arges_dbtype = txt_pi_db_type.Text;
                mIniCfg.MPiview_arges_maxnum = txt_pi_max_num.Text.Trim();
                mIniCfg.MPiview_arges_userid = txt_pi_username.Text.Trim();
                mIniCfg.MPiview_arges_userpwd = txt_pi_password.Text.Trim();

                mIniCfg.MPrint_font = txt_font_print.Text.Trim();
                mIniCfg.MPrint_font_size = txt_font_size_print.Text.Trim();

                mIniCfg.MReport_font = txt_report_font.Text.Trim();
                mIniCfg.MReport_font_size = txt_font_size_report.Text.Trim();
                mIniCfg.MReportToPiview = cbk_report_ropiview.Checked == true ? "1" : "0";

                mIniCfg.f_save_ini(mIniCfg);

                //刷新界面配置参数
                MMain_other.f_loadIniCfg(mIniCfg);

                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("REPORT错误", "保存失败:" + ex.Message);
            }
        }

        Fr_main_other mMain_other;

        public Fr_main_other MMain_other
        {
            get { return mMain_other; }
            set { mMain_other = value; }
        }

        Model_cfg mIniCfg = new Model_cfg();

        private void uc_IniCfg_Load(object sender, EventArgs e)
        {
            //加载INI配置信息
            mIniCfg.MRelive_path = AccessHelper.IniReadValue("SERVER", "RELATE_PATH", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MRelive_path))
            {

                AccessHelper.IniWriteValue("SERVER", "RELATE_PATH", "", Environment.CurrentDirectory + @"\sysconfig.ini");

            }

            if (mIniCfg.MRelive_path == "1")
            {
                ckb_relative_path.Checked = true;

            }
            else
            {
                ckb_relative_path.Checked = false;
            }

            //调用的列名
            mIniCfg.MPiview_column = AccessHelper.IniReadValue("SERVER", "PIVIEW_COLUMNE", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPiview_column))
            {

                AccessHelper.IniWriteValue("SERVER", "PIVIEW_COLUMNE", "PATIENT_ID", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPiview_column = "patient_id";
            }

            txt_pi_arges.Text = "病人号";// mIniCfg.MPiview_column.ToLower();
            // txt_pi_arges.SE
            //PIVIEW数据库路径
            mIniCfg.MDbpath = AccessHelper.IniReadValue("SERVER", "MDBPATH", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MDbpath))
            {

                AccessHelper.IniWriteValue("SERVER", "MDBPATH", "", Environment.CurrentDirectory + @"\sysconfig.ini");
            }
            txt_pi_db_path.Text = mIniCfg.MDbpath;


            //report数据库路径
            mIniCfg.MDbpath_report = AccessHelper.IniReadValue("SERVER", "MDBPATH_REPORT", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MDbpath_report))
            {

                AccessHelper.IniWriteValue("SERVER", "MDBPATH_REPORT", "", Environment.CurrentDirectory + @"\sysconfig.ini");
            }

            txt_report_path.Text = mIniCfg.MDbpath_report;
            //PIVIEW_PATH
            mIniCfg.MPiview_path = AccessHelper.IniReadValue("SERVER", "PIVIEW_PATH", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPiview_path))
            {

                AccessHelper.IniWriteValue("SERVER", "PIVIEW_PATH", "", Environment.CurrentDirectory + @"\sysconfig.ini");
            }

            txt_piview_exe_path.Text = mIniCfg.MPiview_path;

            mIniCfg.MPiview_arges_dbtype = AccessHelper.IniReadValue("SERVER", "PIVIEW_ARGES_DBTYPE", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPiview_arges_dbtype))
            {

                AccessHelper.IniWriteValue("SERVER", "PIVIEW_ARGES_DBTYPE", "0", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPiview_arges_dbtype = "0";
            }

            txt_pi_db_type.Text = mIniCfg.MPiview_arges_dbtype;

            mIniCfg.MPiview_arges_maxnum = AccessHelper.IniReadValue("SERVER", "PIVIEW_ARGES_MAXNUM", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPiview_arges_maxnum))
            {

                AccessHelper.IniWriteValue("SERVER", "PIVIEW_ARGES_MAXNUM", "3", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPiview_arges_maxnum = "3";
            }

            txt_pi_max_num.Text = mIniCfg.MPiview_arges_maxnum;

            mIniCfg.MPiview_arges_userid = AccessHelper.IniReadValue("SERVER", "PIVIEW_ARGES_USERID", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPiview_arges_userid))
            {

                AccessHelper.IniWriteValue("SERVER", "PIVIEW_ARGES_USERID", "Administrator", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPiview_arges_userid = "Administrator";
            }

            txt_pi_username.Text = mIniCfg.MPiview_arges_userid;

            mIniCfg.MPiview_arges_userpwd = AccessHelper.IniReadValue("SERVER", "PIVIEW_ARGES_USEPWD", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPiview_arges_userpwd))
            {

                AccessHelper.IniWriteValue("SERVER", "PIVIEW_ARGES_USEPWD", "", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPiview_arges_userid = "";
            }
            txt_pi_password.Text = mIniCfg.MPiview_arges_userpwd;

            mIniCfg.MPiview_arges = AccessHelper.IniReadValue("SERVER", "PIVIEW_ARGES", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPiview_arges))
            {

                AccessHelper.IniWriteValue("SERVER", "PIVIEW_ARGES", "+*+|+*+*+*+*+*+*+*+*+", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPiview_arges = "+*+|+*+*+*+*+*+*+*+*+";
            }



            mIniCfg.MPrint_font = AccessHelper.IniReadValue("SERVER", "PRINT_FONT", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPrint_font))
            {

                AccessHelper.IniWriteValue("SERVER", "PRINT_FONT", "宋体", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPrint_font = "宋体";
            }
            txt_font_print.Text = mIniCfg.MPrint_font;

            mIniCfg.MPrint_font_size = AccessHelper.IniReadValue("SERVER", "PRINT_FONT_SIZE", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MPrint_font_size))
            {

                AccessHelper.IniWriteValue("SERVER", "PRINT_FONT_SIZE", "12", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPrint_font = "12";
            }
            txt_font_size_print.Text = mIniCfg.MPrint_font_size;
            mIniCfg.MReport_font = AccessHelper.IniReadValue("SERVER", "REPORT_FONT", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MReport_font))
            {

                AccessHelper.IniWriteValue("SERVER", "REPORT_FONT", "宋体", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MReport_font = "宋体";
            }

            txt_report_font.Text = mIniCfg.MReport_font;

            mIniCfg.MReport_font_size = AccessHelper.IniReadValue("SERVER", "REPORT_FONT_SIZE", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MReport_font_size))
            {

                AccessHelper.IniWriteValue("SERVER", "REPORT_FONT_SIZE", "12", Environment.CurrentDirectory + @"\sysconfig.ini");
                mIniCfg.MPrint_font = "12";
            }
            txt_font_size_report.Text = mIniCfg.MReport_font_size;

            mIniCfg.MCode = AccessHelper.IniReadValue("SERVER", "CODE", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MReport_font_size))
            {

                AccessHelper.IniWriteValue("SERVER", "CODE", "", Environment.CurrentDirectory + @"\sysconfig.ini");

            }

            mIniCfg.MReportToPiview = AccessHelper.IniReadValue("SERVER", "REPORTTOPIVIEW", Environment.CurrentDirectory + @"\sysconfig.ini");

            if (string.IsNullOrEmpty(mIniCfg.MReportToPiview))
            {

                AccessHelper.IniWriteValue("SERVER", "REPORTTOPIVIEW", "", Environment.CurrentDirectory + @"\sysconfig.ini");
            }

            if (mIniCfg.MReportToPiview == "1")
            {
                cbk_report_ropiview.Checked = true;
            }
        }


        private void btn_piview_exe_path_Click(object sender, EventArgs e)
        {
            //string localFilePath, fileNameExt, newFileName, FilePath; 
            OpenFileDialog mPiExe_FileDialog = new OpenFileDialog();

            //设置文件类型 
            //mPiExe_FileDialog. = "执行文件(*.exe)|*.exe";

            //设置默认文件类型显示顺序 
            //  mPiExe_FileDialog.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            // mPiExe_FileDialog. = true;

            //点了保存按钮进入 
            if (mPiExe_FileDialog.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = mPiExe_FileDialog.FileName.ToString(); //获得文件路径 
                txt_piview_exe_path.Text = localFilePath;
            }
        }



        private void btn_piview_db_path_Click(object sender, EventArgs e)
        {
            //string localFilePath, fileNameExt, newFileName, FilePath; 
            OpenFileDialog mPiDb_FileDialog = new OpenFileDialog();
            //设置文件类型 
            //mPiDb_FileDialog.Filter = "数据文件(*.mdb)|*.mdb|";

            ////设置默认文件类型显示顺序 
            //mPiDb_FileDialog.FilterIndex = 1;

            ////保存对话框是否记忆上次打开的目录 
            //mPiDb_FileDialog.RestoreDirectory = true;

            //点了保存按钮进入 
            if (mPiDb_FileDialog.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = mPiDb_FileDialog.FileName.ToString(); //获得文件路径 
                txt_pi_db_path.Text = localFilePath;
            }
        }

        private void btn_report_db_path_Click(object sender, EventArgs e)
        {
            //string localFilePath, fileNameExt, newFileName, FilePath; 
            OpenFileDialog mPiReportDb_FileDialog = new OpenFileDialog();

            //设置文件类型 
            //mPiReportDb_FileDialog.Filter = "数据文件(*.mdb)|*.mdb|";

            ////设置默认文件类型显示顺序 
            //mPiReportDb_FileDialog.FilterIndex = 1;

            ////保存对话框是否记忆上次打开的目录 
            //mPiReportDb_FileDialog.RestoreDirectory = true;
            //mPiReportDb_FileDialog.RootFolder=
            //点了保存按钮进入 
            if (mPiReportDb_FileDialog.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = mPiReportDb_FileDialog.FileName.ToString(); //获得文件路径 
                txt_report_path.Text = localFilePath;
            }
        }

        private void ckb_relative_path_CheckedChanged(object sender, EventArgs e)
        {
            //根据当前程序目录得到其他路径，浏览路径按钮不可点击
            if (ckb_relative_path.Checked)
            {
                txt_piview_exe_path.Text = "";
                btn_piview_exe_path.Enabled = false;

                txt_pi_db_path.Text = "";
                btn_piview_db_path.Enabled = false;

                //txt_report_path.Text = "";
                //btn_report_db_path.Enabled = false;

                //填写路径
                txt_piview_exe_path.Text = System.AppDomain.CurrentDomain.BaseDirectory + @"PiView.exe";
                txt_pi_db_path.Text = System.Environment.CurrentDirectory.Substring(0, System.Environment.CurrentDirectory.LastIndexOf('\\')) + "\\Database\\PiView.mdb";
                //txt_report_path.Text = System.AppDomain.CurrentDomain.BaseDirectory + @"PiViewReport.mdb"; ;
            }
            else
            {
                txt_piview_exe_path.Text = mIniCfg.MPiview_path;
                btn_piview_exe_path.Enabled = true;

                txt_pi_db_path.Text = mIniCfg.MDbpath;
                btn_piview_db_path.Enabled = true;

                //txt_report_path.Text = mIniCfg.MDbpath_report;
                //btn_report_db_path.Enabled = true;
            }

        }



        private void txt_pi_arges_SelectedIndexChanged(object sender, EventArgs e)
        {
            mIniCfg.MPiview_arges = "";
            string mArges = "";

            //
            foreach (DevExpress.XtraEditors.Controls.RadioGroupItem item in txt_pi_arges.Properties.Items)
            {
                if (txt_pi_arges.Text == item.Value.ToString())
                {
                    mArges = mArges + "+|";
                }
                else
                {
                    mArges = mArges + "+*";
                }
            }

            mIniCfg.MPiview_arges = mArges + "+";
        }

        public void f_setParams(string pText)
        {
            txt_pi_arges.EditValue = pText;
        }










    }
}
